# -*- coding=utf-8
from DingTalkApi import ToDingTalk
from QcloudApi import PutDataQcloud
from TarFile import tarGzDir
from Qdelete import FileDeleteQcloud
from DateTime import getday

from time import ctime
import datetime
import sys

# 拼接压缩文件名
monthNow = datetime.datetime.now().month
if monthNow < 10:
    monthNow = "0"+str(monthNow)
dateTimeName = str(datetime.datetime.now().year)+"-"+monthNow+"-"+str(datetime.datetime.now().day)+".tar.gz"
# 发送信息
TalkText = "你的网页已经备份，备份名称为 "+dateTimeName+" ！"
# 上传文件的绝对路径
PATH = "/tmp/"+dateTimeName
# 上传至腾讯云服务器的路径
QPATH = "/back/"+dateTimeName

def ToText():
    ToDingTalk(TalkText)
	
def putData():
    PutDataQcloud(QPATH, PATH)
	
def tarData():
    tarGzDir("/webdata", dateTimeName, "/tmp")
	
def deleteOldFile():
    yr = datetime.datetime.now().year
    mo = datetime.datetime.now().month
    dy = datetime.datetime.now().day
    OldFile = "/back/"+str(getday(yr,mo,dy,-3))+".tar.gz"
    try:
        FileDeleteQcloud(OldFile)   
    except Exception:
        print("腾讯对象存储文件删除失败！")
        sys.exit(0)

def main():
    tarData()
    putData()
    ToText()
    deleteOldFile()

	
if __name__ == '__main__':
  main()
